package android.pot.com.smartcar.ui.my.activity;

import android.os.Bundle;
import android.os.Handler;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.action.Action;
import android.pot.com.smartcar.base.MyResponseHandler;
import android.pot.com.smartcar.base.swipe.SwipeBackActivity;
import android.pot.com.smartcar.fragment.CallDialogView;
import android.pot.com.smartcar.tool.LoadingTool;
import android.pot.com.smartcar.tool.LoginTool;
import android.pot.com.smartcar.tool.TipToast;
import android.pot.com.smartcar.ui.login.LoginsActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhaoshilu on 2017/11/27.
 */
public class MyUpdatePwdActivity extends SwipeBackActivity {

    private EditText et_password,et_new_password,et_sure_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_pwd);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        initView();
        addListener();
        initData();
    }

    @Override
    protected void initView() {
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("修改密码");
        et_password = (EditText) findViewById(R.id.et_password);
        et_new_password = (EditText) findViewById(R.id.et_new_password);
        et_sure_password = (EditText) findViewById(R.id.et_sure_password);

    }


    @Override
    protected void addListener() {
        findViewById(R.id.img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollToFinishActivity();
            }
        });

        findViewById(R.id.bt_commit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                validText();

            }
        });
    }

    @Override
    protected void initData() {

    }

    private void validText(){
        if (TextUtils.isEmpty(et_password.getText().toString().trim())){
            TipToast.show("请输入原密码");
            return;
        }
        if (et_password.getText().toString().trim().length()<6 || et_password.getText().toString().trim().length()>12){
            TipToast.show("请输入6-12位密码");
            return;
        }
        if (TextUtils.isEmpty(et_new_password.getText().toString().trim())){
            TipToast.show("请输入新密码");
            return;
        }
        if (et_new_password.getText().toString().trim().length()<6 || et_new_password.getText().toString().trim().length()>12){
            TipToast.show("请输入6-12位新密码");
            return;
        }
        if (TextUtils.isEmpty(et_sure_password.getText().toString().trim())){
            TipToast.show("请再次输入新密码");
            return;
        }
        if (et_sure_password.getText().toString().trim().length()<6 || et_sure_password.getText().toString().trim().length()>12){
            TipToast.show("请输入6-12位确认密码");
            return;
        }
        if (!et_new_password.getText().toString().trim().equals(et_sure_password.getText().toString().trim())){
            TipToast.show("新密码和确认密码不一致");
            return;
        }
        getDetailData();

    }
    private void getDetailData(){
        Map<String,String> params = new HashMap<>();
        params.put("password_y",et_password.getText().toString());
        params.put("password",et_new_password.getText().toString());
        params.put("password_confirm",et_sure_password.getText().toString());
        LoadingTool.StartLoading(this);
        Action.get().change_password(this, params, new MyResponseHandler() {
            @Override
            public void onSuccess(String message,JSONObject response) {
                LoadingTool.EndLoading(MyUpdatePwdActivity.this);
                CallDialogView dialogView = new CallDialogView(MyUpdatePwdActivity.this,"密码修改成功");
                dialogView.show(getSupportFragmentManager(),"dialogView");
                dialogView.setMethodClickListener(new CallDialogView.OnMethodClickListener() {
                    @Override
                    public void methodClick() {
                        LoginTool.setLoginOut(MyUpdatePwdActivity.this);
                        startAct(MyUpdatePwdActivity.this,LoginsActivity.class);

                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                scrollToFinishActivity();
                            }
                        }, 1000);
                    }
                });

            }
        });
    }


}